<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="180px">
      <el-form-item label="招生年份" prop="year">
        <el-input
          v-model="queryParams.year"
          placeholder="请输入招生年份"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="选择导师" prop="insId">
        <el-select
          v-model="queryParams.insId"
          placeholder="选择导师"
          clearable
          filterable
        >
          <el-option v-for="ins in instructorList" :label="ins.insName" :value="ins.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="选择学科名" prop="subId">
        <el-select v-model="queryParams.subId" filterable clearable placeholder="选择学科名" >
          <el-option v-for="subject in subjectList" :label="subject.subName" :value="subject.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="招生名额数" prop="pgNumber">
        <el-input
          v-model="queryParams.pgNumber"
          placeholder="请输入招生名额数"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['mentor:spot:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['mentor:spot:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['mentor:spot:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['mentor:spot:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="spotList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" v-if="false"/>
      <el-table-column label="招生年份" align="center" prop="year" />
      <el-table-column label="导师" align="center" prop="insId" >
        <template slot-scope="scope">
          <el-link @click="$router.push(`/instructor/detail/${scope.row.insId}`)" type="primary">{{instructorMap[scope.row.insId]}}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="学科名" align="center" prop="subId" width="180" >
        <template slot-scope="scope">
          <el-tag type="warning">{{subjectMap[scope.row.subId]}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="已招生数" align="center" prop="checkedNumber" />
      <el-table-column label="招生名额数" align="center" prop="pgNumber" />
      <el-table-column label="审核状态" align="center" prop="examineStatus" width="120" >
        <template slot-scope="scope">
          <el-tag type="info" v-if="scope.row.examineStatus==0">未审核</el-tag>
          <el-tag v-if="scope.row.examineStatus==1">审核中</el-tag>
          <el-tag type="danger" v-if="scope.row.examineStatus==2">审核未通过</el-tag>
          <el-tag type="success" v-if="scope.row.examineStatus==3">审核通过</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createDate" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center" prop="updateDate" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="400">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.examineStatus==1"
            size="small"
            type="success"
            icon="el-icon-edit"
            @click="examineSpot(scope.row.id,3)"
            v-hasPermi="['mentor:spot:edit']"
          >审核通过</el-button>
          <el-button
            v-if="scope.row.examineStatus==1"
            size="small"
            type="danger"
            icon="el-icon-edit"
            @click="examineSpot(scope.row.id,2)"
            v-hasPermi="['mentor:spot:edit']"
          >审核不通过</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['mentor:spot:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mentor:spot:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改招生名额对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="招生年份" prop="year">
          <el-input v-model="form.year" placeholder="请输入招生年份" />
        </el-form-item>
        <el-form-item label="选择导师" prop="insId">
          <el-select v-model="form.insId" placeholder="选择导师" clearable filterable>
            <el-option v-for="ins in instructorList" :label="ins.insName" :value="ins.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="学科名" prop="subId">
          <el-select v-model="form.subId" placeholder="学科名" >
            <el-option v-for="subject in subjectList" :label="subject.subName" :value="subject.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="招生名额数" prop="pgNumber">
          <el-input v-model="form.pgNumber" placeholder="招生名额数" />
        </el-form-item>

        <el-form-item label="审核状态" prop="examineStatus">
          <el-select v-model="form.examineStatus" placeholder="审核状态" clearable >
            <el-option v-for="status in examineStatusOptions" :value="status.value" :label="status.label"></el-option>
          </el-select>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSpot, getSpot, delSpot, addSpot, updateSpot, exportSpot,examineSpot } from "@/api/mentor/spot";
import {getAllSubject, getAllSubjectMap} from "../../../api/mentor/subject";
import {getAllInstructor} from '../../../api/mentor/instructor'

export default {
  name: "Spot",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 招生名额表格数据
      spotList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        year: undefined,
        insId: undefined,
        subId: undefined,
        pgNumber: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        year: [
          { required: true, message: "招生年份不能为空", trigger: "blur" }
        ],
        insId: [
          { required: true, message: "导师ID不能为空", trigger: "blur" }
        ],
        subId: [
          { required: true, message: "学科点编号不能为空", trigger: "blur" }
        ]
      },
      subjectList:[],
      subjectMap:[],
      instructorList:[],
      instructorMap:{},
      examineStatusOptions: [
        {
          label:'未审核',
          value:0
        },
        {
          label:'审核中',
          value:1
        },
        {
          label:'审核未通过',
          value:2
        },
        {
          label:'审核已通过',
          value:3
        },
      ]
    };
  },
  async created() {
    await this.getAllSubject()
    await this.getAllSubjectMap()
    await this.getList();
    await this.getAllInstructor()
  },
  methods: {
    /** 查询招生名额列表 */
    getList() {
      this.loading = true;
      listSpot(this.queryParams).then(response => {
        this.spotList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        year: undefined,
        insId: undefined,
        subId: undefined,
        pgNumber: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加招生名额";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSpot(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改招生名额";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSpot(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSpot(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除招生名额编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delSpot(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有招生名额数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportSpot(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    },
    async getAllSubjectMap() {
      const res = await getAllSubjectMap()
      // console.log(res)
      this.subjectMap = res.data
    },
    async getAllSubject(){
      const res = await getAllSubject()
      this.subjectList = res.data
    },
    //获取所有导师信息
    async getAllInstructor() {
      const res = await getAllInstructor()
      this.instructorList = res.data
      this.instructorMap = {}
      res.data.forEach(item=>{
        let key = item.id
        this.instructorMap[key] = item.insName
      })
    },
    //审核学科招生信息
    async examineSpot(id, status) {
      const res = await examineSpot(id, status)
      this.getList()
    }
  }
};
</script>
